<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>WycTransmitIndex</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>

    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <style>

        ul,li{ padding:0;margin:0;list-style:none}
        .tag{ color: red}
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span12">
                <input id="keyword" class="input-medium col-lg-6" type="text" />
                <button id="searchBtn" type="submit" class="btn">好耶</button>
                <span style="color: red">输入题目内容点击按钮或者回车（文字题就复制题目、图片题右键图片复制图片链接然后粘贴）</span>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span12">
                
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>
                                ID
                            </th>
                            <th>
                                题目
                            </th>
                            <th>
                                题型
                            </th>
                            <th>
                                选项列表
                            </th>
                            <th>
                                答案
                            </th>
                        </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>
<script>
    $(function(){

        $("#searchBtn").on("click",function(){
            search();
        });

        $("#keyword").keydown(function(e){
            console.log(e.keyCode);
            if (e.keyCode == 13){
                search();
            }
        });

        function search(){
            let html = '<tr><td style="text-align: center" colspan="5">让聪明的大葱在思考一会。。。（超过1秒没反应就是卡了，再点一下）</td></tr>'
            $('tbody').html(html)
            $.get("/search",
                {
                    keyword: $("#keyword").val()
                },
                function (data,status) {
                    var html = '';

                    for (let i = 0; i < data.length; i++) {
                        let item = data[i];
                        html += "<tr>"

                        html += "<td>" +item.id +"</td>"
                        html += "<td>" +item.stem +"</td>"

                        if (item.answerMode == 'SingleSelection'){
                            html += "<td>单选题</td>"
                        }else if (item.answerMode == 'Judgement'){
                            html += "<td>判断题</td>"
                        }

                        html += "<td>";
                        html += "<ul>";
                        let first = item.optionList[0].id;
                        for (let j = 0; j < item.optionList.length; j++) {
                            let optionItem = item.optionList[j];

                            if (item.answer == optionItem.id){
                                html += "<li class='tag'>";
                            }else{
                                html += "<li>";
                            }



                            let option = 'unknow';
                            if (first == '0'){
                                switch (optionItem.id){
                                    case '0':
                                        option = 'A';
                                        break
                                    case '1':
                                        option = 'B';
                                        break
                                    case '2':
                                        option = 'C';
                                        break
                                    case '3':
                                        option = 'D';
                                        break
                                    default:
                                        option = optionItem.id;
                                        break
                                }
                            }else{
                                switch (optionItem.id){
                                    case '1':
                                        option = 'A';
                                        break
                                    case '2':
                                        option = 'B';
                                        break
                                    case '3':
                                        option = 'C';
                                        break
                                    case '4':
                                        option = 'D';
                                        break
                                    default:
                                        option = optionItem.id;
                                        break
                                }
                            }

                            html += option + " "+optionItem.content;
                            html += "</li>";
                        }
                        html += "</ul>";
                        html += "</td>"

                        let option = 'unknow';
                        if (first == '0'){
                            switch (item.answer){
                                case '0':
                                    option = 'A';
                                    break
                                case '1':
                                    option = 'B';
                                    break
                                case '2':
                                    option = 'C';
                                    break
                                case '3':
                                    option = 'D';
                                    break
                                default:
                                    option = item.answer;
                                    break
                            }
                        }else{
                            switch (item.answer){
                                case '1':
                                    option = 'A';
                                    break
                                case '2':
                                    option = 'B';
                                    break
                                case '3':
                                    option = 'C';
                                    break
                                case '4':
                                    option = 'D';
                                    break
                                default:
                                    option = item.answer;
                                    break
                            }
                        }
                        html += "<td>" +option +"</td>"

                        html += "</tr>";
                    }

                    $('tbody').html(html)
                    console.log(data)
                })
        }


    });
</script>